/*****************************************************************
                        Highest Retun Ratio Next
PURPOSE: This is the algorithm for highest return ratio next CPU 
scheduling. It inherits data and functionalities from base class Scheduler.
*****************************************************************/
import java.util.ArrayList;

public class HRRN extends Scheduler implements Runnable {   

   /*--------------------------------------------------------
                        Constructor
   PURPOSE:  To ask help from views, set up data, and begin simulation
   PARAMTERS: references the input queue, stats and anime view. gets
   value of starting clock time.
   --------------------------------------------------------*/
   public HRRN(ArrayList<Process> q, statsFrame s, animeFrame a, inputFrame i, int c) {
      super(q,s,a,i,c);
    
      thread = new Thread(this,"HRRN");
      thread.start();
   } // constructor

   /*--------------------------------------------------------
                        Thread Run
   PURPOSE:  To run light-weight thread simulation
   --------------------------------------------------------*/
   public void run() {
      int all=queue.size();
      double rr;
      ArrayList<RRinfo> rrQ = new ArrayList<RRinfo>();
      RRinfo RRtemp,RR=null;

      do {
         clock++;
         for (int j=0; j<rrQ.size(); j++)
            rrQ.get(j).delay();                       
         T = processready(clock);
         if (T != null) {
            readyQueue.add(T);
            rrQ.add(new RRinfo(T));
            queue.remove(T);
            an.upstatus("Time "+T.getArrival()+":Process "+T.getName()+" ready.");
            try { Thread.sleep(1000); } catch (InterruptedException e) {};
         } // put in ready queue
         if (idle) {
            if (readyQueue.size()==0)
               continue;
            idle = false;
            rr = -1; // dummy init value
            for (int j=0; j<readyQueue.size(); j++) {
               RRtemp = search(readyQueue.get(j), rrQ);
               if (rr < RRtemp.getRR()) {
                  RR = RRtemp;                  
                  P = RR.getProc();
                  rr = RR.getRR();
               } // find shortest process next
            } // search readyQ
            rrQ.remove(RR);
            readyQueue.remove(P);
         } // put in run state
         P.servicing();
         an.drawbar(P,clock);
         an.upstatus("Time "+clock+":Serving process "+P.getName()+".");
         try { Thread.sleep(1000); } catch (InterruptedException ex) {};
         if (P.getTminus()==0) {
            an.upstatus("Time "+(clock+1)+":Process "+P.getName()+" done.");
            try { Thread.sleep(1000); } catch (InterruptedException e) {};
            P.report(clock+1); // anticipate completion
            finishQueue.add(P);
            idle = true;
         } // put in finish queue
      } while (finishQueue.size()<all);
      an.upstatus("Algorithm finished.");
      st.report(finishQueue,"Highest Response Ratio Next");
      in.resetGUI();
   } // run thread

   /*--------------------------------------------------------
                        Search utility
   PURPOSE:  To find the corresponding augmented process information
   PARAMTERS: references process to be found and possible queue to find it in
   --------------------------------------------------------*/
   private RRinfo search(Process A, ArrayList<RRinfo> X) {
      for (int j=0; j<X.size(); j++)
         if (A == X.get(j).getProc())
            return X.get(j);
      return null;
   } // finds RRobject for process
} // HRRN class
